package com.qcu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcu.dto.StaffAccountDto;
import com.qcu.entity.StaffAccount;
import com.qcu.exception.RepeatException;
import com.qcu.service.StaffAccountService;
import com.qcu.mapper.StaffAccountMapper;
import com.qcu.util.PasswordUtils;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author MyComputer
* @description 针对表【staff_account】的数据库操作Service实现
* @createDate 2025-02-06 09:21:10
*/
@Service
public class StaffAccountServiceImpl extends ServiceImpl<StaffAccountMapper, StaffAccount>
    implements StaffAccountService{

    @Resource
    private StaffAccountMapper staffAccountMapper;

    /**
     * 保存员工信息
     *
     * @param staffAccountDto
     * @return
     */
    @Override
    public Response saveStaff(StaffAccountDto staffAccountDto)  {
        try {
            StaffAccount staffAccount = new StaffAccount();
            String salt = PasswordUtils.generateSalt();
            String hashPassword = PasswordUtils.hashPassword(staffAccountDto.getPassword(), salt);
            BeanUtils.copyProperties(staffAccountDto,staffAccount);
            staffAccount.setType(Integer.valueOf(staffAccountDto.getType()));
            staffAccount.setSalt(salt);
            staffAccount.setPasswordHash(hashPassword);
            int insert = staffAccountMapper.insert(staffAccount);
            if (insert > 0) {
                return Response.success();
            } else {
                return Response.failure(ResponseCode.FAILURE, "添加失败");
            }
        } catch (BeansException e) {
            throw new RuntimeException(e);
        } catch (NumberFormatException e) {
            throw new RuntimeException(e);
        }catch (Exception e){
            throw new RepeatException(e.getMessage());
        }
    }

    /**
     * 更新员工信息
     *
     * @param staffAccountDto
     * @return
     */
    @Override
    public Response updateStaff(StaffAccountDto staffAccountDto) {
        StaffAccount staffAccount = new StaffAccount();
        BeanUtils.copyProperties(staffAccountDto,staffAccount);
        if (staffAccountDto.getPassword() != null && !staffAccountDto.getPassword().equals("")){
            String salt = PasswordUtils.generateSalt();
            String hashPassword = PasswordUtils.hashPassword(staffAccountDto.getPassword(), salt);
            staffAccount.setSalt(salt);
            staffAccount.setPasswordHash(hashPassword);
        }
        staffAccount.setType(Integer.valueOf(staffAccountDto.getType()));
        int update = staffAccountMapper.updateById(staffAccount);
        if (update > 0) {
            return Response.success();
        } else {
            return Response.failure(ResponseCode.FAILURE, "更新失败");
        }
    }
}




